<?php

class MyAccountController extends Zend_Controller_Action{

	protected $auth_instance;
	
    public function init(){
        $this->auth_instance = Zend_Auth::getInstance();
    }

    public function indexAction(){
    	if (!$this->auth_instance->hasIdentity()){
    		$this->view->myAccountContents = "<h3>Nu sunteti logat sau contul .dvs nu exista! </h3>";
    		$this->view->myAccountContents.= '<a href="/myAccount/create">Creeaza-ti un cont!</a>';
    		$this->view->myAccountContents.= '<br />';
    		$this->view->myAccountContents.= '<a href="admin/login">Intra in contul tau</a>';
    	} else {
    		//TODO account infos with change option
    		$this->view->myAccountContents = "you are logged in";
    	}
    }
    
    public function createAction(){
    	$request = $this->getRequest();
    	$form = new Application_Form_AddNormalUser();
    	
    	if ($this->getRequest()->isPost()) {
    		if ($form->isValid($request->getPost())) {
    			
    			$user = new Application_Model_User();
    			$user->setLast_name($form->getValue('last_name'));
    			$user->setFirst_name($form->getValue('first_name'));
    			$user->setEmail($form->getValue('email'));
    			$user->setAddress($form->getValue('address'));
    			$user->setPassword($form->getValue('password'));    			
    			
    			$mapper = new Application_Model_UserMapper();
    			$mapper->save($user);
    			
    			//send mail to user
		   		$front = Zend_Controller_Front::getInstance();
		   		$bootstrap = $front->getParam('bootstrap');
		    	$smtpSettings = $bootstrap->getOption('smtp');
		    	
		    	$config = array('ssl' => $smtpSettings['ssl'],
		    			'username' => $smtpSettings['username'],
		    			'password' => $smtpSettings['password'],
		    			'port' => $smtpSettings['port'],
		    			'auth' => $smtpSettings['auth']);
		    	
		    	$transport = new Zend_Mail_Transport_Smtp( $smtpSettings['smtpServer'], $config );
		    	$orderMail = new Zend_Mail();
		    	Zend_Mail::setDefaultTransport($transport);
		    	 
		    	$orderMail->setSubject("Bun venit pe eStore!");
		    	$orderMail->setBodyText("Contul .dvs a fost creat!");
		    	$orderMail->setFrom("admin@estore.com","Cumparaturi pe eStore");
		    	$orderMail->addTo('ctln.pgs@gmail.com','ctln.pgs@gmail.com');
		    	
		    	try{
		    		
		    		$orderMail->send();
		    	}catch (Zend_Exception $e){
		    		$this->view->mailSendException = $e->getMessage();
		    	}
    			
		    	$this->view->accountCreated = 'Contul a fost creat! Un e-mail cu datele a fost trimis pentru confirmare.';
		    	$this->view->accountCreted.= 'Acum va puteti loga in contul .dvs --> <a href="admin/login">Login</a>';
    			return $this->_helper->redirector('create');
    		}
    	}
    	$this->view->form = $form;
    }

}

